package com.hgyc.mom.tool.controller;

import java.util.*;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.hgyc.mom.tool.entity.JobLog;
import com.hgyc.mom.tool.vo.JobLogParam;
import com.hgyc.mom.tool.service.JobLogService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 定时任务调度日志Controller
 *
 * @author fwj
 * @since 2025-05-08
 */
@Tag(name = "定时任务调度日志控制器", description = "定时任务调度日志控制器")
@RestController
@RequestMapping("/tool/jobLog")
public class JobLogController {
    @Autowired
    private JobLogService jobLogService;

    @Operation(summary = "查询定时任务调度日志列表", description = "查询定时任务调度日志列表")
    @PostMapping("/pageList")
    public Result<IPage<JobLog>> pageList(@Parameter(description = "查询参数") @RequestBody JobLogParam jobLogParam) {
        Page<JobLog> page = new Page<>(jobLogParam.getPage(), jobLogParam.getPageSize());
        JobLog jobLog = new JobLog();
        BeanUtils.copyProperties(jobLogParam, jobLog);
        QueryWrapper<JobLog> wrapper = QueryWrapperUtil.build(jobLog);
        IPage<JobLog> list = jobLogService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "导出定时任务调度日志列表", description = "导出定时任务调度日志列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobLogParam jobLogParam) {
        JobLog jobLog = new JobLog();
        BeanUtils.copyProperties(jobLogParam, jobLog);
        QueryWrapper<JobLog> wrapper = QueryWrapperUtil.build(jobLog);
        List<JobLog> list = jobLogService.list(wrapper);
        ExcelUtil<JobLog> util = new ExcelUtil<>(JobLog.class);
        util.exportExcel(response, list, "定时任务调度日志数据");
    }

    @Operation(summary = "获取定时任务调度日志详细信息", description = "获取定时任务调度日志详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "定时任务调度日志ID") @PathVariable("id") Long id) {
        return Result.success(jobLogService.getById(id));
    }

    @Operation(summary = "新增定时任务调度日志", description = "新增定时任务调度日志")
    @PostMapping
    public Result save(@Parameter(description = "定时任务调度日志") @RequestBody JobLog jobLog) {
        return Result.success(jobLogService.save(jobLog));
    }

    @Operation(summary = "修改定时任务调度日志", description = "修改定时任务调度日志")
    @PutMapping
    public Result update(@Parameter(description = "定时任务调度日志") @RequestBody JobLog jobLog) {
        return Result.success(jobLogService.updateById(jobLog));
    }

    @Operation(summary = "删除定时任务调度日志", description = "删除定时任务调度日志")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(jobLogService.removeByIds(Arrays.asList(ids)));
    }
}